package com.vnp.vchat.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Typeface;
import android.graphics.Bitmap.Config;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.vnp.vchat.R;
import com.vnp.vchat.UIControls.MyCircleImageView;
import com.vnp.vchat.bean.ObjContact;
import com.vnp.vchat.util.Utils;

public class ContactAdapter extends ArrayAdapter<ObjContact> implements
ListAdapter {
	private static final int TYPE_CLICK = 1;
	private static final int TYPE_NOMAL = 0;
	boolean is_select_contact = false;
	Context context;
	Holder holder;
	ArrayList<ObjContact> ObjContacts = new ArrayList<ObjContact>();
	DisplayImageOptions options = new DisplayImageOptions.Builder()
			.cacheInMemory(false).cacheOnDisc(true)
			.showImageForEmptyUri(R.drawable.defaul_avatar)
			.showImageOnFail(R.drawable.defaul_avatar)
			.bitmapConfig(Config.RGB_565)
			.displayer(new FadeInBitmapDisplayer(300)).build();

	private LayoutInflater inflater;

	public ContactAdapter(Context context, ArrayList<ObjContact> ObjContacts) {
		super(context, R.layout.item_search_result, ObjContacts);
		this.context = context;
		this.ObjContacts = ObjContacts;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return ObjContacts.size();
	}

	@Override
	public ObjContact getItem(int position) {
		return ObjContacts.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public ArrayList<ObjContact> getSelectedGroup() {
		ArrayList<ObjContact> checked = new ArrayList<ObjContact>();
		for (ObjContact groupObject : ObjContacts) {
			if (groupObject.getCheck()==1) {
				checked.add(groupObject);
			}
		}
		return checked;
	}

	@Override
	public View getView(final int position, View v, ViewGroup parent) {
		holder = null;
		if (v == null) {
			
			int check= getItemViewType(position);
			if(check==TYPE_CLICK)v = inflater.inflate(R.layout.item_contact_choose, parent, false);
			else v = inflater.inflate(R.layout.item_contact, parent, false);
			
			holder = new Holder();

			holder.imgAvatar = (MyCircleImageView) v
					.findViewById(R.id.img_avatar);
			holder.txtUseName = (TextView) v.findViewById(R.id.use_name);
			holder.txtLoginName = (TextView) v.findViewById(R.id.login_name);
			holder.imgAvatar.setVisibility(View.VISIBLE);
			holder.imageView1 = (ImageView) v.findViewById(R.id.imageView1);
			holder.ll_contact = (LinearLayout) v.findViewById(R.id.ll_contact);
			final ObjContact obj = ObjContacts.get(position);

			
			v.setTag(holder);
		} else {
			holder = (Holder) v.getTag();
		}
		
		
			
			
		holder.txtUseName.setText(ObjContacts.get(position).getName());
		holder.txtLoginName.setText(ObjContacts.get(position).getPhone());
		holder.txtUseName.setTypeface(Utils.getTypefaceRegular(context));
		holder.txtLoginName.setTypeface(Utils.getTypefaceLight(context), Typeface.NORMAL);
		holder.imageView1.setTag(position);
		return v;
	}

	public ArrayList<String> getphone() {
		ArrayList<String> namePhone = new ArrayList<String>();

		return namePhone;
	}

	class Holder {
		MyCircleImageView imgAvatar;
		TextView txtUseName;
		TextView txtLoginName;
		boolean is_check;
		ImageView imageView1;
		LinearLayout ll_contact;

	}

	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public int getItemViewType(int position) {

		int ck = ObjContacts.get(position).getCheck();

		return ck;

	}

	class HeaderViewHolder {
		TextView text;
	}

//	@Override
//	public View getHeaderView(int position, View convertView, ViewGroup parent) {
//		HeaderViewHolder holder;
//		if (convertView == null) {
//			holder = new HeaderViewHolder();
//			convertView = inflater.inflate(R.layout.header_listcontact, parent,
//					false);
//			holder.text = (TextView) convertView.findViewById(R.id.text);
//			convertView.setTag(holder);
//		} else {
//			holder = (HeaderViewHolder) convertView.getTag();
//		}
//		// set header text as first char in name
//		String headerText = ""
//				+ ObjContacts.get(position).getName().subSequence(0, 1)
//						.charAt(0);
//		holder.text.setText(headerText);
//		return convertView;
//	}
//
//	@Override
//	public long getHeaderId(int position) {
//		return ObjContacts.get(position).getName().subSequence(0, 1).charAt(0);
//	}

}
